package com.ymatou.iapi.refund.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.refund.parameter.CreateReturnBillBean;
import com.ymatou.iapi.refund.parameter.CreateReturnBillResp;
import com.ymatou.iapi.refund.parameter.GetBuyerReturnInfoByOrderIdBean;
import com.ymatou.iapi.refund.parameter.GetReturnInfoByReturnIdBean;
import com.ymatou.iapi.refund.parameter.GetReturnInfoByReturnIdResp;
import com.ymatou.iapi.refund.parameter.RefundLogVo;
import com.ymatou.iapi.refund.parameter.RefundProductVo;
import com.ymatou.iapi.refund.parameter.ReturnBillInfoList;
import com.ymatou.iapi.refund.service.CreateReturnBillCall;
import com.ymatou.iapi.refund.service.GetBuyerReturnInfoByOrderIdCall;
import com.ymatou.iapi.refund.service.GetReturnInfoByReturnIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.RefundCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.CS_ReturnApplyBill;
import com.ymttest.database.model.CS_ReturnApplyBillLog;
import com.ymttest.database.model.CS_ReturnApplyPictures;
import com.ymttest.database.model.CS_ReturnApplyProducts;
import com.ymttest.database.sqlwapper.CS_FastReturnLogWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillLogWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyPicturesWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyProductsWapper;
/**
*根据退单号查询退货退款详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetReturnInfoByReturnId{
	private static GetReturnInfoByReturnIdBean bean;
	private static GetReturnInfoByReturnIdCall call;
	private static CreateReturnBillCall createCall;
    private static CreateReturnBillBean createBean;
    private static CS_ReturnApplyBillWapper returnApplyBillWapper=new CS_ReturnApplyBillWapper();
	private static CS_ReturnApplyProductsWapper returnApplyProducts=new CS_ReturnApplyProductsWapper();
	private static CS_ReturnApplyPicturesWapper returnApplyPictures=new CS_ReturnApplyPicturesWapper();
	private static CS_ReturnApplyBillLogWapper returnApplyBillLog=new CS_ReturnApplyBillLogWapper();
	private static CS_FastReturnLogWapper fastReturnLog=new CS_FastReturnLogWapper();
	private SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据退单号查询退货退款详情");
	}

	@Before
	public void caseUp() {
		bean=new GetReturnInfoByReturnIdBean();
		call=new GetReturnInfoByReturnIdCall();
		createCall=new CreateReturnBillCall();
		createBean=new CreateReturnBillBean();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_001() {
		Logger.start(true,"正例-根据退单号查询退货退款详情");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			call.setData(bean);
			call.callService();			
			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_002() {
		Logger.start(true,"正例-根据退单号查询退货退款详情-多个商品");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithTwoProcutInOneOrder(false, false);
			
			createBean=service.setCreateReturnBillMoreBean_AcceptOrder(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			call.setData(bean);
			call.callService();			
			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_003() {
		Logger.start(true,"正例-根据退单号查询退货退款详情-订单不接单，直接取消");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			call.setData(bean);
			call.callService();			
			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_004() {
		Logger.start(true,"正例-根据退单号查询退货退款详情-主动取消");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			List<Order> orders=tradingServiceResp.getOrderList();
            long orderId=orders.get(0).getOrderId();
            long userId=tradingServiceResp.getUserId();
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			//取消订单
			TradingCallService.cancelOrder(orderId,CancelOrderTypeEnum.BUYER_CANCEL_ORDER,userId);
            
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(createBean.getUserid());
			call.setData(bean);
			call.callService();			
			verifyReturn();
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_005() {
		Logger.start(false,"正例-根据退单号查询退货退款详情-退单号为空");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setReturnId("");
			bean.setUserid(createBean.getUserid());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals(true, call.getMsg1().contains("refundId:退款单号不能为空"), "验证为空的refundId");
			Logger.verifyEquals("400", call.getStatus(), "验证无效的Status");
			Logger.verifyEquals("12", call.getbCode(), "验证无效的bCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_006() {
		Logger.start(false,"正例-根据退单号查询退货退款详情-退单号不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setReturnId("9999999");
			bean.setUserid(createBean.getUserid());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("", call.getMsg1(), "验证不存在的refundId");
			Logger.verifyEquals("200", call.getStatus(), "验证无效的Status");
			Logger.verifyEquals("0", call.getbCode(), "验证无效的bCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetReturnInfoByReturnId_007() {
		Logger.start(false,"正例-根据退单号查询退货退款详情-userId不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserid(99999);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals(true, call.getMsg1().contains("非法的用户Id"), "验证不存在的userId");
			Logger.verifyEquals("500", call.getStatus(), "验证无效的Status");
			Logger.verifyEquals("4", call.getbCode(), "验证无效的bCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn()
	{
		
		GetReturnInfoByReturnIdResp resp=call.getCallData();
		
		Logger.verifyEquals("",call.getMsg1(), "验证Msg");
		Logger.verifyEquals("200",call.getStatus(), "验证Status");
		Logger.verifyEquals("0",call.getbCode(), "验证bCode");
		CheckCS_ReturnApplyBill(resp);
		CheckCS_ReturnProducts(resp);
		CheckCS_PicUrls(resp);
		CheckCS_ReturnApplyBillLog(resp);
		
		//Logger.verifyEquals(resp.getIsRecevied(),returnApplyBill.get(), "验证CS_ReturnApplyBill表ReturnFreihtAmount是否正确");
        //RejectTimes
		//ReturnHistoryStatus
	}
	
	private void CheckCS_ReturnApplyBill(GetReturnInfoByReturnIdResp resp)
	{
		CS_ReturnApplyBill returnApplyBill=returnApplyBillWapper.selectByApplyBillId(bean.getReturnId()).get(0);
		Logger.verifyEquals(resp.getOrderId(), returnApplyBill.getOrderId(), "验证CS_ReturnApplyBill表OrderId是否正确");
		Logger.verifyEquals(resp.getSellerId(), returnApplyBill.getSellerId(), "验证CS_ReturnApplyBill表BuyerId是否正确");
		Logger.verifyEquals(resp.getSellerLoginId(), returnApplyBill.getSellerLoginId(), "验证CS_ReturnApplyBill表SellerLoginId是否正确");
		Logger.verifyEquals(resp.getRefundAmount().toString(), returnApplyBill.getReturnAmount().stripTrailingZeros().toPlainString(), "验证CS_ReturnApplyBill表ReturnAmount是否正确");
		
       Logger.verifyEquals(resp.getReturnRemark(),returnApplyBill.getReturnRemark(), "验证CS_ReturnApplyBill表ReturnRemark是否正确");
		
		Logger.verifyEquals(resp.getExpressBillNumber(),returnApplyBill.getExpressBillNumber(), "验证CS_ReturnApplyBill表ExpressBillNumber是否正确");
		Logger.verifyEquals(resp.getExpressBillNumber(),returnApplyBill.getExpressBillNumber(), "验证CS_ReturnApplyBill表ExpressBillNumber是否正确");
		Logger.verifyEquals(resp.getExpressCoCode(),returnApplyBill.getExpressCoCode(), "验证CS_ReturnApplyBill表ExpressCoCode是否正确");
		Logger.verifyEquals(resp.getExpressCoName(),returnApplyBill.getExpressCoName(), "验证CS_ReturnApplyBill表ExpressCoName是否正确");
		Logger.verifyEquals(resp.getReceiverName(),returnApplyBill.getReceiver(), "验证CS_ReturnApplyBill表ReceiverName是否正确");
		Logger.verifyEquals(resp.getReceiverPhone(),returnApplyBill.getReceiverPhone(), "验证CS_ReturnApplyBill表ReceiverPhone是否正确");
		Logger.verifyEquals(resp.getReturnAddress(),returnApplyBill.getReturnAddress(), "验证CS_ReturnApplyBill表ReturnAddress是否正确");
		Logger.verifyEquals(resp.getAddTime(),format1.format(returnApplyBill.getAddTime()), "验证CS_ReturnApplyBill表AddTime是否正确");
		Logger.verifyEquals(resp.getUpdateTime(),format1.format(returnApplyBill.getUpdateTime()), "验证CS_ReturnApplyBill表UpdateTime是否正确");
		Logger.verifyEquals(resp.getStatus(),returnApplyBill.getAction(), "验证CS_ReturnApplyBill表Action是否正确");
		
		Logger.verifyEquals(resp.getIsLocalReturn(),returnApplyBill.getIsLocalReturn(), "验证CS_ReturnApplyBill表IsLocalReturn是否正确");
		if(returnApplyBill.getIsOfficialAddress()==null )
		Logger.verifyEquals(resp.getIsOfficalAddress(),false, "验证CS_ReturnApplyBill表IsOfficialAddress是否正确");
		else
			Logger.verifyEquals(resp.getIsOfficalAddress(),returnApplyBill.getIsOfficialAddress(), "验证CS_ReturnApplyBill表IsOfficialAddress是否正确");
		Logger.verifyEquals(resp.getReturnFreihtAmount().toString(),returnApplyBill.getReturnFreihtAmount().stripTrailingZeros().toPlainString(), "验证CS_ReturnApplyBill表ReturnFreihtAmount是否正确");
		Logger.verifyEquals(resp.getIsCancelOrder(),returnApplyBill.getIsCancelOrder(), "验证CS_ReturnApplyBill表IsCancelOrder是否正确");
		
		Logger.verifyEquals(resp.getRefundAmount().toString(),returnApplyBill.getReturnAmount().stripTrailingZeros().toPlainString(), "验证CS_ReturnApplyBill表ReturnAmount是否正确");
		Logger.verifyEquals(resp.getReturnReasonId(),returnApplyBill.getReturnReasonId(), "验证CS_ReturnApplyBill表ReturnReasonId是否正确");
	//	Logger.verifyEquals(resp.getApplyReason(),returnApplyBill.getResponsableRemark(), "验证CS_ReturnApplyBill表ReturnRemark是否正确");
		Logger.verifyEquals(resp.getApplyType(),returnApplyBill.getAccepterType(), "验证CS_ReturnApplyBill表AccepterType是否正确");
	}
	
	private void CheckCS_ReturnProducts(GetReturnInfoByReturnIdResp resp)
	{
		//ReturnProducts
		List<CS_ReturnApplyProducts> pList=returnApplyProducts.selectByApplyBillId(bean.getReturnId());
		List<RefundProductVo> resproductList=call.getCallData().getReturnProducts();
		Logger.verifyEquals(pList.size(), resproductList.size(), "验证CS_ReturnApplyProducts表与返回的数量一致");
		for(int i=0;i<pList.size();i++)
		{
			Logger.verifyEquals(resproductList.get(i).getProductId(), pList.get(i).getProductId(), "验证CS_ReturnApplyProducts表ProductId是否正确");
			Logger.verifyEquals(resproductList.get(i).getCatalogId(), pList.get(i).getCatalogId(), "验证CS_ReturnApplyProducts表CatalogId是否正确");
			Logger.verifyEquals(resproductList.get(i).getCatalogText(), pList.get(i).getCatalogText(), "验证CS_ReturnApplyProducts表CatalogText是否正确");
			Logger.verifyEquals(resproductList.get(i).getProductName(), pList.get(i).getProductName(), "验证CS_ReturnApplyProducts表ProductName是否正确");
			Logger.verifyEquals(resproductList.get(i).getProductPrice().toString(), pList.get(i).getProductPrice().stripTrailingZeros().toPlainString(), "验证CS_ReturnApplyProducts表ProductPrice是否正确");
			
			Logger.verifyEquals(resproductList.get(i).getPictureUrl(), pList.get(i).getPicUrl(), "验证CS_ReturnApplyProducts表PictureUrl是否正确");
			Logger.verifyEquals(resproductList.get(i).getRefundCount(), pList.get(i).getReturnCount(), "验证CS_ReturnApplyProducts表ReturnCount是否正确");
			//Logger.verifyEquals(resproductList.get(i).getOrderCount(), pList.get(i).getReturnCount(), "验证CS_ReturnApplyProducts表OrderCount是否正确");
		}
	}
	
	private void CheckCS_PicUrls(GetReturnInfoByReturnIdResp resp)
	{
		//PicUrls
				List<CS_ReturnApplyPictures> picList=returnApplyPictures.selectByApplyBillId(bean.getReturnId());
				List<String> respicList=call.getCallData().getPicUrls();
				Logger.verifyEquals(respicList.size(), picList.size(), "验证CS_ReturnApplyPictures表与返回的数量一致");
				for(int j=0;j<picList.size();j++)
				{
					Logger.verifyEquals(respicList.get(j),picList.get(j).getImgUrl(), "验证CS_ReturnApplyPictures表ImgUrl是否正确");
				}
	}
	
	private void CheckCS_ReturnApplyBillLog(GetReturnInfoByReturnIdResp resp)
	{
		List<CS_ReturnApplyBillLog> logList=returnApplyBillLog.selectByApplyBillId(bean.getReturnId());
		List<RefundLogVo> respLogList=call.getCallData().getReturnHistoryStatus();
		for(int k=logList.size();k<logList.size();k--)
		{
			Logger.verifyEquals(respLogList.get(k).getOperatorId(),logList.get(k).getOperatorId(), "验证CS_ReturnApplyBillLog表OperatorId是否正确");
			
			Logger.verifyEquals(respLogList.get(k).getOperatorType(),logList.get(k).getOperatorType(), "验证CS_ReturnApplyBillLog表OperatorType是否正确");
			Logger.verifyEquals(respLogList.get(k).getOperatorName(),logList.get(k).getOperatorName(), "验证CS_ReturnApplyBillLog表OperatorName是否正确");
			
			Logger.verifyEquals(respLogList.get(k).getAccepterLoginId(),logList.get(k).getAccepterLoginId(), "验证CS_ReturnApplyBillLog表AccepterLoginId是否正确");
			Logger.verifyEquals(respLogList.get(k).getAccepterType(),logList.get(k).getAccepterType(), "验证CS_ReturnApplyBillLog表AccepterType是否正确");
			Logger.verifyEquals(respLogList.get(k).getApplyType(),logList.get(k).getType(), "验证CS_ReturnApplyBillLog表ApplyType是否正确");
			//List<CS_ReturnApplyBill> applyList=returnApplyBillWapper.selectByApplyBillId(bean.getReturnId());
			Logger.verifyEquals(respLogList.get(k).getStatus(),logList.get(k).getReturnBillAction(), "验证CS_ReturnApplyBillLog表OrderStatus是否正确");
			Logger.verifyEquals(respLogList.get(k).getRemark(),logList.get(k).getRemark(), "验证CS_ReturnApplyBillLog表Remark是否正确");
			Logger.verifyEquals(respLogList.get(k).getUpdateRemark(),logList.get(k).getReturnRemark(), "验证CS_ReturnApplyBillLog表UpdateRemark是否正确");
			Logger.verifyEquals(respLogList.get(k).getAddTime(),format1.format(logList.get(k).getAddTime()), "验证CS_ReturnApplyBillLog表AddTime是否正确");
			Logger.verifyEquals(respLogList.get(k).GetIsCommunication(),false, "验证true:留言；false:日志记录");
			//Times
			//CloseType
		}
	}
	
}